<?php

session_start();
ob_start();
include_once dirname(dirname(__FILE__)) . '/include/common.php';
$page = isset($_POST["page"]) ? intval($_POST["page"]) : 1;

$core = new core();
$load_more = 0;
$zen_order_id = isset($_POST["zen_order_id"]) ? intval($_POST["zen_order_id"]) : 0;
$by_product_customer = isset($_POST["by_product_customer"]) ? intval($_POST["by_product_customer"]) : 0;
$by_order_status = isset($_POST["by_order_status"]) ? intval($_POST["by_order_status"]) : 0;
$by_province = isset($_POST["by_province"]) ? intval($_POST["by_province"]) : 0;
$by_category = isset($_POST["by_category"]) ? intval($_POST["by_category"]) : 0;
$by_payment = isset($_POST["by_payment"]) ? intval($_POST["by_payment"]) : 0;
$search_text = isset($_POST["search_text"]) ? addslashes(trim($_POST["search_text"])) : '';
$start_time = isset($_POST["start_time"]) ? addslashes(trim($_POST["start_time"])) : '';
$end_time = isset($_POST["end_time"]) ? addslashes(trim($_POST["end_time"])) : '';
$order_by = '';
if ($by_product_customer == 0 && $by_order_status == 0 && $by_province == 0 && $by_payment == 0 && $search_text != "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng chọn 1 trong 4 sự lựa chọn để sắp xếp!"));
    die;
}
if ($start_time != "" && $end_time != "") {
    if (strtotime($end_time) <= strtotime($start_time)) {
        echo json_encode(array('type' => 1, 'msg' => "Bạn phải chọn thời gian kết thúc lớn hơn thời gian bắt đầu!"));
        die;
    }
}
if ($start_time != "") {
    $start_time = $core->changeDateMysqlToCompare(date("Y-m-d H:i:s", strtotime($start_time)));
}
if ($end_time != "") {
    $end_time = $core->changeDateMysqlToCompare(date("Y-m-d H:i:s", strtotime($end_time)));
}
$where = '';
if ($by_order_status > 0) {
    switch ($by_order_status) {
        case 1:
            //store
            $where.='order_status=0';
            if ($start_time != "") {
                $where.=' AND store_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND store_date<"' . $end_time . '"';
            }
            $order_by .= "store_date asc";
            break;
        case 2:
            //online
            $where.='order_status=1';
            if ($start_time != "") {
                $where.=' AND online_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND online_date<"' . $end_time . '"';
            }
            $order_by .= "online_date asc";
            break;
        case 3:
            //hàng có sẵn
            $where.='order_status=2';
            break;
        case 4:
            //đã nhận hàng
            $where.='order_status=3';
            if ($start_time != "") {
                $where.=' AND received_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND received_date<"' . $end_time . '"';
            }
            $order_by .= "received_date asc";
            break;
        case 5:
            //đang shipping
            $where.='order_status=4';
            break;
        case 6:
            //khách đã nhận
            $where.='order_status=5';
            if ($start_time != "") {
                $where.=' AND saled_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND saled_date<"' . $end_time . '"';
            }
            $order_by .= "saled_date asc";
            break;
        case 7:
            //hết hàng
            $where.='order_status=6';
            break;
        case 8:
            //ORDER
            $where.='order_status=7';
            if ($start_time != "") {
                $where.=' AND customer_order_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND customer_order_date <"' . $end_time . '"';
            }
            $order_by .= "customer_order_date asc";
            break;
        case 9:
            //chưa chắc chắn
            $where.='order_status=8';
            break;
    }
} else {
    if ($start_time != "") {
        if ($where == '') {
            $where.='(store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        } else {
            $where.=' AND (store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        }
    }
    if ($end_time != "") {
        if ($where == '') {
            $where.='(store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        } else {
            $where.=' AND (store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        }
    }
}
if ($order_by != "") {
    //ORDER: sắp xếp ngày đặt mua tăng dần
    if ($by_product_customer == 1) {
        $order_by .= ", product_code asc";
    } elseif ($by_product_customer == 2) {
        $order_by .= ", facebook_name asc";
    }
} else {
    if ($by_product_customer == 1) {
        $order_by .= "product_code asc";
    } elseif ($by_product_customer == 2) {
        $order_by .= "facebook_name asc";
    }
}
if ($by_payment > 0) {
    switch ($by_payment) {
        case 1:
            //thanh toán hết
            if ($where == '') {
                $where.='total_current_money=0';
            } else {
                $where.=' AND total_current_money=0';
            }
            break;
        case 2:
            //thanh toán thừa
            if ($where == '') {
                $where.='total_current_money > 0';
            } else {
                $where.=' AND total_current_money > 0';
            }
            break;
        case 3:
            //thanh toán thiếu
            if ($where == '') {
                $where.='total_current_money < 0';
            } else {
                $where.=' AND total_current_money < 0';
            }
            break;
        case 4:
            //đã cọc
            if ($where == '') {
                $where.='total_payment > 0';
            } else {
                $where.=' AND total_payment > 0';
            }
            break;
        case 5:
            //chưa thanh toán
            if ($where == '') {
                $where.='total_payment = 0';
            } else {
                $where.=' AND total_payment = 0';
            }
            break;
    }
}

if ($search_text !== "") {
    if ($by_product_customer == 1) {
        if ($where == '') {
            $where.="product_code LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND product_code LIKE '%" . $search_text . "%'";
        }
    } elseif ($by_product_customer == 2) {
        if ($where == '') {
            $where.="facebook_name LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND facebook_name LIKE '%" . $search_text . "%'";
        }
    }
}
//filter by category
if($by_category > 0){
    if ($where == '') {
        $where.="product_category = $by_category";
    } else {
        $where.=" AND product_category = $by_category";
    }
}
//filter by province
if ($by_province > 0) {
    switch ($by_province) {
        case 1:
            //tỉnh
            if ($where == '') {
                $where.='province=1';
            } else {
                $where.=' AND province=1';
            }
            break;
        case 2:
            //Hà Nội
            if ($where == '') {
                $where.='province=0';
            } else {
                $where.=' AND province=0';
            }
            break;
    }
}
$last_customer_order_id = isset($_POST["last_customer_order_id"]) ? intval($_POST["last_customer_order_id"]) : 0;
$var_count = isset($_POST["var_count"]) ? intval($_POST["var_count"]) : 0;
if ($core->checkZenOrderByID($zen_order_id)) {

    $total_result = array();
    if ($by_order_status > 0 || $by_product_customer == 1) {
        $is_group_customer = 0;
    } else {
        $is_group_customer = 1;
    }
    $list_customer_order = $core->getListCustomerOrderFilterByCustomer($zen_order_id, $order_by, $where, $page, $load_more, $total_result, 20);
    $customer_order_id = 0;
    $html_total_result = '';
    if ($_SESSION["user_role"] != 3) {
        include_once '../templates/ajax/full_permission_order_filter.php';
    } else {
        include_once '../templates/ajax/view_permission_order_filter.php';
    }

    $page = $page + 1;
    echo json_encode(array('type' => 0, 'html' => $html, 'page' => $page, 'load_more' => $load_more, 'last_customer_order_id' => $customer_order_id, 'html_total_result' => $html_total_result, 'var_count' => $var_count));
    die;
}
?>